﻿using lsw_VoteSys.Models.Database;
using lsw_VoteSys.service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace lsw_VoteSys.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : Controller
    {
        //定义数据库上下文
        private readonly lsw_VoteSysContext _db;
        //注入jwt服务
        private readonly IJWTService _jwtService;

        /// <summary>
        /// 构造方法 构造注入
        /// </summary>
        public UserController(lsw_VoteSysContext db, IJWTService jwtService)
        {
            _db = db;
            _jwtService = jwtService;
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        [HttpPost]
        public string UserRegister(string Email, string Pwd, string ConfirmPwd)
        {
            if (Pwd == ConfirmPwd)
            {
                string result = "";
                MD5 md5 = MD5.Create();
                byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(ConfirmPwd));
                for (int i = 0; i < s.Length; i++)
                {
                    result = result + s[i].ToString("X2");
                }
                _db.Users.Add(new User()
                {
                    Email = Email,
                    Password = result,
                });
                var row = _db.SaveChanges();
                if (row > 0)
                    return "注册成功";
                return "注册失败";
            }
            return "未知错误";
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        [HttpPost]
        public string UserLogin(string Email, string Pwd)
        {
            if (Email != null && Pwd != null)
            {
                string result = "";
                MD5 md5 = MD5.Create(); 
                byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(Pwd));
                for (int i = 0; i < s.Length; i++)
                {
                    result = result + s[i].ToString("X2");
                }
                var User = _db.Users.FirstOrDefault(x => x.Email == Email);
                if (User != null)
                {
                    if (User.Password == result)
                    {
                        string Token = _jwtService.CreateToken(Email);
                        return "登录成功：" + Token;
                    }
                    return "登录失败：请查看信息是否正确";
                }
                return "没有该用户信息，请先注册";
            }
            return "用户名或密码不能为空！";
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string UserInfo()
        {
            //获取用户信息
            return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        }
    }
}
